//MINI UI解析
mini.parse();

//导航配置处理器类
function NavCfgHandler() {
	this.sncType = mini.get("sncType");
	this.sncName = mini.get("sncName");
	this.sncPage = mini.get("sncPage");
	this.sncPosition = mini.get("sncPosition");
	this.sncState = mini.get("sncState");
	this.navCfgGrid = mini.get("navCfgGrid");
	
	this.isExpanded = false;
	this.ncCtl = mini.get("ncCtl");
	this.tbForm = $("#toolbarForm");
	this.ncPanel = mini.get("navConfigPanel");
	
	this.wmCfg = new WebMgrConfig();
	this.queryType = this.wmCfg.navType[0].value;
	this.queryName = "";
	this.queryPage = this.wmCfg.normalPage[0].value;
	this.queryPosition = this.wmCfg.navPosition[0].value;
	this.queryState = this.wmCfg.normalState[0].value;
	
	this.navCfgGrid.url = __root + "/getNavConfigs.do";
	
	this.loadSncType = function() {
		this.sncType.setData(this.wmCfg.navType);
		this.sncType.setValue(this.wmCfg.navType[0].value);
	};
	
	this.loadSncPage = function() {
		this.sncPage.setData(this.wmCfg.normalPage);
		this.sncPage.setValue(this.wmCfg.normalPage[0].value);
	};
	
	this.loadSncPosition = function() {
		this.sncPosition.setData(this.wmCfg.navPosition);
		this.sncPosition.setValue(this.wmCfg.navPosition[0].value);
	};
	
	this.loadSncState = function() {
		this.sncState.setData(this.wmCfg.normalState);
		this.sncState.setValue(this.wmCfg.normalState[0].value);
	};
	
	this.loadNavCfgGrid = function() {
		var param = {
			sncType : this.queryType, 
			sncName : this.queryName, 
			sncPage : this.queryPage, 
			sncPosition : this.queryPosition, 
			sncState : this.queryState};
		this.navCfgGrid.load(param);
	};
	
	this.findType = function(value) {
		var type = null;
		
		for (var index = 0; index < this.wmCfg.navType.length; index += 1) {
			var temp = this.wmCfg.navType[index];
			if (temp.value == value) {
				type = temp;
				break;
			}
		}
		
		return type;
	};
	
	this.findPage = function(value) {
		var page = null;
		
		for (var index = 0; index < this.wmCfg.normalPage.length; index += 1) {
			var temp = this.wmCfg.normalPage[index];
			if (temp.value == value) {
				page = temp;
				break;
			}
		}
		
		return page;
	};
	
	this.findPosition = function(value) {
		var position = null;
		
		for (var index = 0; index < this.wmCfg.navPosition.length; index += 1) {
			var temp = this.wmCfg.navPosition[index];
			if (temp.value == value) {
				position = temp;
				break;
			}
		}
		
		return position;
	};
	
	this.findState = function(value) {
		var state = null;
		
		for (var index = 0; index < this.wmCfg.normalState.length; index += 1) {
			var temp = this.wmCfg.normalState[index];
			if (temp.value == value) {
				state = temp;
				break;
			}
		}
		
		return state;
	};
	
	this.changeToYes = function(id) {
		this.switchState(id, true);
	};
	
	this.changeToNo = function(id) {
		this.switchState(id, false);
	};
	
	this.switchState = function(id, to) {
		var param = { sncId : id, sncTo : to};
		var json = mini.encode(param);
		$.ajax({
			url : __root + "/switchNcState.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadNavCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.edit = function(id, name) {
		miniWin.afreshNewWin();
		miniWin.setParams(620, 396, "编辑配置[<font color='blue'>" + name + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("editNavCfg.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { isedit : true, sncId : id };
	        iframe.contentWindow.instance.load(data);
		}, function(action) {
			if (action == "save") {
				instance.loadNavCfgGrid();
			}
		});
		
		miniWin.show();
	};
	
	this.del = function(id) {
		mini.confirm("确定要删除选定的配置？", "系统提示", function(action) {
			if (action == "ok") {
				var ids = [{ sncId : id }];
				instance.removes(ids);
			}
		});
	};
	
	this.removes = function(ids) {
		var json = mini.encode(ids);
		$.ajax({
			url : __root + "/delNavConfigs.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadNavCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.doLayout = function() {
		instance.ncPanel.doLayout();
	};
};

var instance = new NavCfgHandler();		//创建导航配置处理器全局实例
instance.loadSncType();					//加载内容类型
instance.loadSncPage();					//加载显示页面
instance.loadSncPosition();				//加载显示位置
instance.loadSncState();				//加载配置状态
instance.loadNavCfgGrid();				//加载配置表格

//定义新建配置点击事件处理程序
function onNcNewClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(620, 396, "新建配置");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editNavCfg.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { isedit : true, sncId : null };
        iframe.contentWindow.instance.load(data);
	}, function(action) {
		if (action == "save") {
			instance.loadNavCfgGrid();
		}
	});
	
	miniWin.show();
}

//定义查询控制点击事件处理程序
function onNcCtlClick(e) {
	if (instance.isExpanded) {
		instance.isExpanded = false;
		instance.ncCtl.setText("展开查询");
		instance.ncCtl.setIconCls("icon-btn-expand");
	} else {
		instance.isExpanded = true;
		instance.ncCtl.setText("折叠查询");
		instance.ncCtl.setIconCls("icon-btn-collapse");
	}
	
	instance.tbForm.toggle(500);
	setTimeout(instance.doLayout, 500);
}

//定义查询点击事件处理程序
function onNcQueryClick(e) {
	instance.queryType = instance.sncType.getValue();
	instance.queryName = instance.sncName.getValue();
	instance.queryPage = instance.sncPage.getValue();
	instance.queryPosition = instance.sncPosition.getValue();
	instance.queryState = instance.sncState.getValue();
	
	instance.loadNavCfgGrid();
}

//定义刷新点击事件处理程序
function afresh(e) {
	//执行查询
	onNcQueryClick();
	//重置布局
	instance.doLayout();
}

//定义导航配置表格行双击事件处理程序
function onNavCfgGridRowDbclick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(620, 396, "配置明细[<font color='blue'>" + e.record.sncName + "</font>]");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editNavCfg.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { isedit : false, sncId : e.record.sncId };
        iframe.contentWindow.instance.load(data);
	});
	
	miniWin.show();
}

//定义导航配置表格单元格绘制事件处理程序
function onNavCfgGridDrawCell(e) {
	var html = "";
	
	switch (e.column.field) {
		case "sncType":
		{
			var type = instance.findType(e.record.sncType);
			if (type) {
				html += type.text;
			}
			break;
		}
		case "sncPage":
		{
			var page = instance.findPage(e.record.sncPage);
			if (page) {
				html += page.text;
			}
			break;
		}
		case "sncPosition":
		{
			var position = instance.findPosition(e.record.sncPosition);
			if (position) {
				html += position.text;
			}
			break;
		}
		case "sncState":
		{
			var state = instance.findState(e.record.sncState);
			if (state) {
				html += "<span><div class='" + state.cls + "' title='" + state.text + 
					"' onclick='instance." + state.method + "(\"" + e.record.sncId + "\");'></div></span>";
			}
			break;
		}
		case "sncOperator":
		{
			var state = instance.findState(e.record.sncState);
			if (state) {
				var id = e.record.sncId;
				
				html += "<span><a style='cursor: pointer;' onclick='instance.edit(\"" + id + 
					"\", \"" + e.record.sncName + "\");'>编辑</a></span>";
				html += "<span>&nbsp;|&nbsp;</span>";
				if (state.deleted) {
					html += "<span><a style='cursor: pointer;' onclick='instance.del(\"" + id + "\");'>删除</a></span>";
				} else {
					html += "<span><a style='cursor: default; color: gray;'>删除</a></span>";
				}
			}
			break;
		}
	}
	
	if (html) e.cellHtml = html;
}
